/** 
 * 根据已知节点获取路径 
 * param1: node
 * param2: tree
 * */
export function getNodePath(value, tree) {
  let path = [];
  function traverse(nodes, parentPath) {
    nodes.forEach(node => {
      const currentPath = parentPath.concat(node.departName);
      if (node.id === value.id) {
        path = currentPath;
      } else if (node.children && node.children.length > 0) {
        traverse(node.children, currentPath);
      }
    });
  }

  traverse(tree, []);
  return path;
}


export function searchTree(node, searchValue) {
  node.text = node.departName
  if (node.id === searchValue) {
    return node; // 找到匹配的节点，返回该节点
  }
  let foundSubtree;
  if (node.children) {
    // 递归查找子节点
    for (let i = 0; i < node.children.length && !foundSubtree; i++) {
      foundSubtree = searchTree(node.children[i], searchValue);
    }
  }
  // 如果在当前节点的子树中找到了匹配项，返回子树；否则返回null
  return foundSubtree || null;
}


